package com.techstar.api.service.public_transport;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.techstar.api.pojo.public_transport.BusTodayDistrictOd5min;
import com.techstar.api.pojo.public_transport.TaxiRealtimeVehicles;
import com.techstar.api.repos.public_transport.BusTodayDistrictOd5minRepos;
import com.techstar.api.repos.public_transport.TaxiRealtimeVehiclesRepos;
import com.techstar.api.service.ConfigProperties;
import com.techstar.api.service.GateWayApiProperties;
import com.techstar.api.service.HttpClientHeper;
import com.techstar.api.service.TrafficApiProperties;
import com.techstar.common.jackson.JacksonMapper;
import com.techstar.common.pojo.InterfaceType;
import com.techstar.common.utils.InterfaceUtils;
import com.techstar.roma.http.GatewayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.List;

/**
 *
 * @author EinIce
 * @description 1.1.9 公交车当前时间某行政区OD分布
 * @date 2021年11月20日
 */
@Slf4j
@Service
public class BusTodayDistrictOd5minService {
    @Autowired
    private BusTodayDistrictOd5minRepos beanRepos;

    private static final ObjectMapper objectMapper = JacksonMapper.instance.objectMapper;

    @Autowired
    private ConfigProperties configProperties; // 配置文件注入

    public void query(){
        GateWayApiProperties gateWayApiProperties = configProperties.romaApiProperties();
        TrafficApiProperties trafficApiProperties = configProperties.btdoApiProperties();
        if(gateWayApiProperties.testMode==true&&!"test".equals(trafficApiProperties.able)
                ||"disable".equals(trafficApiProperties.able)) {
            log.info("DistrictCongestTrendService out");
            return;
        }
        try {
            String currentTime = URLEncoder.encode(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"), "UTF-8").replaceAll("\\+", "%20");
            trafficApiProperties.param = String.format("current_time=%s", currentTime);
            HttpGet httpGet = new HttpGet();
            httpGet = (HttpGet) GatewayService.GateWay(httpGet,gateWayApiProperties, trafficApiProperties);
            CloseableHttpResponse response = null;
            String result = null;
            response = HttpClientHeper.instance.httpclient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
            log.info(result);
            JsonNode jsonNode = objectMapper.readTree(result.toString()).get("data");
            try {
                InterfaceType.GIS_PUBLIC_TRANSPORT_BUS_TODAY_DISTRICT_OD_5MIN.setApiUrl(httpGet.toString());
                InterfaceType.GIS_PUBLIC_TRANSPORT_BUS_TODAY_DISTRICT_OD_5MIN.setClassName(this.getClass().getSimpleName());
                InterfaceUtils.filtrateDate(InterfaceType.GIS_PUBLIC_TRANSPORT_BUS_TODAY_DISTRICT_OD_5MIN, DateTime.now(), jsonNode);
            } catch (Exception e) {
                log.error("接口记录失败-"+this.getClass().getSimpleName()+":"+e.getMessage());
            }
            if(jsonNode==null)return;
            List<BusTodayDistrictOd5min> list = objectMapper.readValue(jsonNode.toString(),new TypeReference<List<BusTodayDistrictOd5min>>() {});
			beanRepos.saveAll(list);
//            list.forEach(l->
//                    {
//                        try{beanRepos.save(l);}catch(Exception e) {e.printStackTrace();}
//                    }
//            );
            response.close();
        }catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
